﻿using DTcms.API.Payment.xdd;
using DTcms.Common;
using System;
using System.Collections.Specialized;
using System.Web.Mvc;
using System.Web.UI;

namespace DTcms.Web.Mvc.api.payment.xdd
{
    public partial class notify_url : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int site_payment_id = 0; //站点支付方式ID
            NameValueCollection collection;
            if (Context.Request.HttpMethod == "POST")
            {
                collection = Context.Request.Form;
            }
            else
            {
                collection = Context.Request.QueryString;
            }
            if (collection.Count > 0)
            {
                string order_no = DTRequest.GetString("out_trade_no").ToUpper(); //获取订单号
                if (order_no.StartsWith("R")) //充值订单
                {
                    site_payment_id = new BLL.user_recharge().GetPaymentId(order_no);
                }
                else if (order_no.StartsWith("B")) //商品订单
                {
                    Response.Redirect(UrlHelperExtend.linkurl("error", "?msg=" + Utils.UrlEncode("B开头为商品订单功能弃用！")));
                    return;
                }
                //找到站点支付方式ID开始验证
                if (site_payment_id > 0)
                {
                    XddpayResponse xddpayresponse = new XddpayResponse(site_payment_id, collection, Context);//创建XddpayResponse实例

                    if (xddpayresponse.IsXddpaySign())//判断签名
                    {
                        //string order_no = xddpayresponse.getParameter("order_no");  //商户自己的订单号
                        string subject = xddpayresponse.getParameter("subject");    //商品名称
                        string pay_type = xddpayresponse.getParameter("pay_type");  //支付类型
                        string money = xddpayresponse.getParameter("money");    //订单金额
                        string realmoney = xddpayresponse.getParameter("realmoney");    //实际支付金额
                        string result = xddpayresponse.getParameter("result");          //支付结果
                        string xddpay_order_no = xddpayresponse.getParameter("xddpay_order");          //小叮当订单号
                        string app_id = xddpayresponse.getParameter("app_id"); //接口app_id
                        string extra = xddpayresponse.getParameter("extra");    //用户自定义数据

                        if ("success".Equals(result))
                        {
                            if (order_no.StartsWith("R")) //充值订单
                            {
                                site_payment_id = new BLL.user_recharge().GetPaymentId(order_no);
                            }
                            else if (order_no.StartsWith("B")) //商品订单
                            {
                                Response.Redirect("B开头为商品订单功能弃用！");
                                return;
                            }
                            if (site_payment_id == 0)
                            {
                                Response.Write("该订单号不存在");
                                return;
                            }

                            if (order_no.StartsWith("R")) //充值订单
                            {
                                BLL.user_recharge bll = new BLL.user_recharge();
                                Model.user_recharge model = bll.GetModel(order_no);
                                if (model == null)
                                {
                                    Response.Write("该订单号不存在");
                                    return;
                                }
                                if (model.status == 1) //已成功
                                {
                                    Response.Write("success");
                                    return;
                                }
                                if (model.amount != decimal.Parse(money))
                                {
                                    Response.Write("订单金额和支付金额不相符");
                                    return;
                                }
                                bool code = bll.Confirm(order_no);
                                if (!code)
                                {
                                    Response.Write("修改订单状态失败");
                                    return;
                                }
                            }
                            else if (order_no.StartsWith("B")) //商品订单
                            {
                                Response.Redirect("B开头为商品订单功能弃用！");
                                return;
                            }
                        }
                        else
                        {
                            Response.Write("订单处理失败");
                        }
                    }
                    else
                    {
                        Response.Write("认证签名失败");
                    }
                }
            }
            //失败状态
            Response.Redirect(UrlHelperExtend.linkurl("payment", "?action=error"));
            return;
        }
    }
}